package main.kotlin.solution

class MinStack {

    class Node(var value:Int,var next:Node?,var pre:Node?)

    val head:Node = Node(0,null,null)
    var topNode:Node = head
    /** initialize your data structure here. */

    fun push(x: Int) {
        topNode.value = x
        if(topNode.next == null){
            topNode.next = Node(0,null,topNode)
        }
        topNode = topNode.next!!
    }

    fun pop() {
        topNode = topNode.pre!!
    }

    fun top(): Int {
        return  topNode.pre?.value ?:0
    }

    fun min(): Int {
        var curNode = head.next
        var minValue = curNode?.value!!
        while(curNode != null){
            if(curNode.value < minValue){
                minValue = curNode.value
            }
        }
        return minValue
    }
}